<template>
  <d2-container>
    <template slot="header">
      <el-form
        :inline="true"
        :model="queryForm"
        ref="queryForm"
        size="mini"
        style="margin-bottom: -18px;"
      >
        <el-form-item label="设备类型" prop="equipmentType">
          <el-select v-model="queryForm.equipmentType" placeholder="请选择" style="width: 100px;">
            <el-option label="请选择" value />
            <el-option label="GENERAL" value="0" />
            <el-option label="LORAWAN" value="1" />
            <el-option label="NBIOT" value="2" />
            <el-option label="RTU" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item prop="searchType">
          <el-select v-model="queryForm.searchType" placeholder="请选择" style="width: 100px;">
            <el-option label="全部" value />
            <el-option label="区域名称" value="0" />
            <el-option label="客户编号" value="1" />
            <el-option label="表计地址" value="2" />
            <el-option label="户名" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label prop="searchVal">
          <el-input v-model="queryForm.searchVal" placeholder="请输入" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="查询日期" prop="dataRange">
          <el-date-picker
            v-model="queryForm.dataRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="fetchData">
            <d2-icon name="search" />查询
          </el-button>
        </el-form-item>
      </el-form>
    </template>
    <template>
      <d2-crud
        ref="d2Crud"
        :columns="columns"
        :data="data"
        :loading="loading"
        :pagination="pagination"
        @pagination-current-change="onPageChange"
      ></d2-crud>
    </template>
    <el-dialog :visible.sync="calledDetailModalFlag" title="指令详情">
      <div class="modal-title">{{calledDetailData.title}}</div>
      <div class="package-total pagekage-line">
        <span>总包号：</span>
        <span>{{calledDetailData.totalNumber}}</span>
      </div>
      <p class="package-content">
        <span>包:</span>
        <span>{{calledDetailData.packageVOS}}</span>
      </p>
    </el-dialog>
    <el-dialog :visible.sync="receiveDetailModalFlag" title="指令详情">
      <div class="modal-title">{{receiveDetailData.title}}</div>
      <div class="package-total">
        <span>总包号：</span>
        <span>{{receiveDetailData.totalNumber}}</span>
        ,
        <span>当前包号：</span>
        <span>{{receiveDetailData.currentNumber}}</span>
      </div>
      <div class="package-total pagekage-line">
        <span>Imei：</span>
        <span>{{receiveDetailData.imei}}</span>
      </div>
      <div class="package-box">
        <div class="package-box-content pagekage-line">
          <span class="package-title">指令内容:</span>
          <span class="package-message">{{receiveDetailData.values}}</span>
        </div>
        <!-- <div class="package-box-content pagekage-line">
          <span class="package-title">客户名称:</span>
          <span class="package-message">{{item.archivesName}}</span>
        </div>
        <div class="package-box-content pagekage-line">
          <span class="package-title">imei:</span>
          <span class="package-message">{{item.imei}}</span>
        </div>
        <div class="package-box-content pagekage-line">
          <span class="package-title">实时时间:</span>
          <span class="package-message">{{item.pnReceiveTime}}</span>
        </div>
        <div class="package-box-content pagekage-line">
          <span class="package-title">接收时间:</span>
          <span class="package-message">{{item.pnSendTime}}</span>
        </div>
        <div class="package-box-content pagekage-line">
          <span class="package-title">累计流量:</span>
          <span class="package-message">{{item.totalStream}}</span>
        </div>
        <div class="package-box-content pagekage-line">
          <span class="package-title">瞬时流量:</span>
          <span class="package-message">{{item.currentStream}}</span>
        </div>
        <div class="package-box-content pagekage-line">
          <span class="package-title">消息:</span>
          <span class="package-message">{{item.message}}</span>
        </div> -->
      </div>
    </el-dialog>
  </d2-container>
</template>

<script>
import dayjs from "dayjs";
export default {
  name: "bootstrapRecord",
  data() {
    return {
      loading: false,
      queryForm: {
        equipmentType: "",
        searchType: "",
        searchVal: "",
        dataRange: []
      },
      columns: [
        {
          title: "序号",
          key: "serialNum",
          width: 50
        },
        {
          title: "表计地址",
          key: "archivesAddress",
          sortable: false
        },
        {
          title: "设备号",
          key: "nbAddress",
          sortable: false
        },
        {
          title: "发送",
          key: "sendDesc",
          sortable: false
        },
        {
          title: "指令发送",
          key: "send",
          sortable: false
        },
        {
          title: "发送时间",
          key: "pnSendTime",
          sortable: true
        },
        {
          title: "发送结果",
          key: "state",
          sortable: false,
          component: {
            render: (h, row, node, index) => {
              return (
                <span
                  onclick={() => {
                    this.clickState(row);
                  }}
                  style="cursor:pointer;color:#409EFF"
                >
                  {row.state}
                </span>
              );
            }
          }
        },
        {
          title: "提交次数",
          key: "doCount",
          sortable: true
        },
        {
          title: "操作人",
          key: "username",
          sortable: false
        },
        {
          title: "接收",
          key: "sendDesc2",
          sortable: false
        },
        {
          title: "指令接收",
          key: "receive",
          sortable: false,
          component: {
            render: (h, row, node, index) => {
              return (
                <span
                  onclick={() => {
                    this.clickReceived(row);
                  }}
                  style="cursor:pointer;color:#409EFF"
                >
                  {row.receive}
                </span>
              );
            }
          }
        },
        {
          title: "接收时间",
          key: "pnReceiveTime",
          sortable: true
        },
        {
          title: "接收结果",
          key: "receiveDesc",
          sortable: false
        }
      ],
      data: [],
      pagination: {
        currentPage: 1,
        pageSize: 100,
        total: 0
      },
      calledDetailData: {},
      calledDetailModalFlag: false,
      receiveDetailData: {},
      receiveDetailModalFlag: false
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    //获取接受信息
    getReceiveDetailDetailData(id) {
      this.$api
        .admin_instruction_receiveDetail({
          id: id
        })
        .then(res => {
          this.receiveDetailModalFlag = true;
          this.receiveDetailData = res;
        })
        .catch(err => {
          console.log("err", err);
        })
        .finally(() => {});
    },
    //点击接收指令
    clickReceived(row) {
      this.getReceiveDetailDetailData(row.id);
    },
    //获取发送-详情
    getCalledDetailData(id) {
      this.$api
        .admin_instruction_calledDetail({
          id: id
        })
        .then(res => {
          this.calledDetailModalFlag = true;
          this.calledDetailData = res;
        })
        .catch(err => {
          console.log("err", err);
        })
        .finally(() => {});
    },
    //点击发送结果row
    clickState(row) {
      this.getCalledDetailData(row.id);
    },
    //删除请求
    deleteRowAction(ids) {
      this.loading = true;
      this.$api
        .admin_log_delete(ids)
        .then(res => {
          this.$message({
            message: "操作成功",
            type: "success"
          });
          this.fetchData();
        })
        .catch(err => {
          console.log("err", err);
        })
        .finally(() => {
          this.loading = false;
        });
    },
    //删除操作
    deleteRow() {
      this.$confirm("确定删除吗?", "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteRowAction(this.selectionRow.map(item => item.id));
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    emptyRowAction() {
      this.loading = true;
      this.$api
        .admin_log_clearAll()
        .then(res => {
          this.$message({
            message: "操作成功",
            type: "success"
          });
          this.fetchData();
        })
        .catch(err => {
          console.log("err", err);
        })
        .finally(() => {
          this.loading = false;
        });
    },
    //清空操作
    emptyRow() {
      this.$confirm("确定清空吗？", "清空", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.emptyRowAction();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    //勾选
    handleSelectionChange(selection) {
      this.selectionRow = selection;
    },
    //分页操作
    onPageChange(currentPage) {
      this.pagination.currentPage = currentPage;
      this.fetchData();
    },
    fetchData() {
      this.loading = true;
      this.$api
        .admin_instruction_getInstructionPageVO({
          pageNum: this.pagination.currentPage,
          pageSize: this.pagination.pageSize,
          equipmentType: this.queryForm.equipmentType,
          beginDate:
            this.queryForm.dataRange && this.queryForm.dataRange.length > 0
              ? dayjs(new Date(this.queryForm.dataRange[0])).format(
                  "YYYY-MM-DD"
                )
              : "",
          endDate:
            this.queryForm.dataRange && this.queryForm.dataRange.length > 0
              ? dayjs(new Date(this.queryForm.dataRange[1])).format(
                  "YYYY-MM-DD"
                )
              : "",
          areaName:
            this.queryForm.searchType == 1 ? this.queryForm.searchVal : "",
          archivesNum:
            this.queryForm.searchType == 2 ? this.queryForm.searchVal : "",
          archivesAddress:
            this.queryForm.searchType == 3 ? this.queryForm.searchVal : "",
          archivesName:
            this.queryForm.searchType == 4 ? this.queryForm.searchVal : ""
        })
        .then(res => {
          console.log(res);
          this.data = res.data;
          this.pagination.total = res.total;
          this.loading = false;
        })
        .catch(err => {
          console.log("err", err);
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleRowRemove({ index, row }, done) {
      this.deleteRowAction([row.id]);
      done();
    },

    handleDialogOpen({ mode, row, index }) {
      if (mode === "edit") {
        this.$api.admin_log_get({ id: row.id }).then(res => {
          this.$refs.d2Crud.setFormData(res);
        });
      }
    },

    handleRowEdit(row, done) {
      this.$message({
        message: "关闭弹框",
        type: "warning"
      });
      done();
    },
    handleDialogCancel(done) {
      this.$message({
        message: "关闭弹框",
        type: "warning"
      });
      done();
    }
  }
};
</script>
<style lang="scss" scoped >
.modal-title {
  font-size: 24px;
}
.package-total {
  font-size: 14px;
  color: gray;
  margin-top: 10px;
}
.pagekage-content {
  font-size: 14px;
  color: gray;
}
.pagekage-line {
  border-bottom: solid 1px gray;
}
.package-box {
  width: 100%;
  display: flex;
  flex-direction: column;
  .package-box-content {
    display: flex;
    flex-direction: row;
    color: gray;
    min-height: 25px;
    line-height: 25px;
    .package-title {
      width: 15%;
      text-align: right;
      font-weight: bold;
      margin-right: 10px;
    }
    .package-message {
      width: 70%;
      text-align: left;
    }
  }
}
</style>
